#pragma once

#include <d3d11_3.h>
#include <wrl.h>
#include <memory>
#include <mutex>

#include "DirectXException.h"

namespace DirectXCore {
    using Microsoft::WRL::ComPtr;

    class Direct3DResource {
    private:
        // DXGI 相关
        ComPtr<IDXGIDevice> mDXGIDevice;
        ComPtr<IDXGIFactory2> mDXGIFactory;

        // D3D11 相关
        ComPtr<ID3D11Device> mD3DDevice;
        ComPtr<ID3D11DeviceContext> mD3DContext;

        // 清理 D3D 资源
        void CleanupResources();
        // 初始化 D3D 资源
        void Initialize();

    public:
        inline Direct3DResource(){}

        ~Direct3DResource() {
            CleanupResources();
        }

        // 禁用拷贝和移动
        Direct3DResource(const Direct3DResource&) = delete;
        Direct3DResource& operator=(const Direct3DResource&) = delete;
        Direct3DResource(Direct3DResource&&) = delete;
        Direct3DResource& operator=(Direct3DResource&&) = delete;

        static std::shared_ptr<Direct3DResource> GetInstance() {
            static auto instance = [] {
                auto ptr = std::make_shared<Direct3DResource>();
                ptr->Initialize();  // 初始化逻辑
                return ptr;
                }();
            return instance;
        }

        // 获取设备接口
        inline ID3D11Device* GetD3DDevice() const {
            if (mD3DDevice == nullptr) throw std::runtime_error("Direct3DResource not initialized");
            return mD3DDevice.Get();
        }

        inline ID3D11DeviceContext* GetD3DContext() const {
            if (mD3DContext == nullptr) throw std::runtime_error("Direct3DResource not initialized");
            return mD3DContext.Get();
        }

        inline IDXGIDevice* GetDXGIDevice() const {
            if (mDXGIDevice == nullptr) throw std::runtime_error("Direct3DResource not initialized");
            return mDXGIDevice.Get();
        }

        inline IDXGIFactory2* GetDXGIFactory() const {
            if (mDXGIFactory == nullptr) throw std::runtime_error("Direct3DResource not initialized");
            return mDXGIFactory.Get();
        }
    };
}

